import DestructibleFacility from "../facility/DestructibleFacility";
import HealthPoint, { Killer } from "../funcObj/interface/HealthPoint";
import NodePoolManager from "../manager/NodePoolManager";
import Player from "../player/Player";

const { ccclass, property } = cc._decorator;

/**
 * 防护罩
 * 先攻击防护罩,再攻击设施
 */
@ccclass
export default class FacilityHood extends cc.Component implements HealthPoint {

    player: Player;

    @property(cc.ProgressBar)
    hpProgress: cc.ProgressBar = null;
    maxHp: number;
    currentHp: number;

    fac: DestructibleFacility = null;

    showFrame: cc.Node = null;

    init(fac: DestructibleFacility): void {
        this.node.children.forEach(node => node.active = false);
        this.fac = fac;
        this.initHp();
        this.fac.node.on(DestructibleFacility.EventType.摧毁, this.die, this);
    }

    initHp() {
        this.player = this.fac.player;
        this.maxHp = this.fac.maxHp;
        this.currentHp = this.maxHp;
        this.hpProgress.node.active = false;
        this.hpProgress.progress = 1;

        switch (this.fac.locations.length) {
            case 1:
                this.showFrame = this.node.children[1];
                this.node.children[1].setContentSize(120, 120);
                break;
            case 2:
                break;
            case 3:
                this.showFrame = this.node.children[3];
                break;
            case 4:
                this.showFrame = this.node.children[1];
                this.showFrame.setContentSize(200, 200);
                break;
        }
        this.showFrame.active = false;
    }

    repairHp(hp: number): void {
    }

    reducedHp(killer: Killer): boolean {
        this.hpProgress.node.active = true;
        this.showFrame.active = true;
        this.currentHp = Math.max(0, this.currentHp - killer.domage);
        this.hpProgress.progress = this.currentHp / this.maxHp;
        if (this.currentHp == 0) {
            this.die(killer);
        }
        return false;
    }

    die(killer: Killer) {
        this.fac.node.targetOff(this);
        NodePoolManager.instance.recycle(this.node);
    }

}